﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;

namespace StudentSystem
{
    public class StudentDao
    {
        public bool insertStudentDao(Student student)
        {
            try
            {
                StreamWriter streamWriter=new StreamWriter("aaa/Student.txt",true);
                string String =student.Id+ ","+student.Name+","+student.Age;
                Console.WriteLine(String);
                streamWriter.WriteLine(String);
                streamWriter.Close();
                return true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                // throw;
                return false;
            }
        }


        public bool deleteStudentDao(int id)
        {
            try
            {
                StreamReader streamReader=new StreamReader("aaa/Student.txt");
                ArrayList students=new ArrayList();
                while (!streamReader.EndOfStream)
                {
                    string String=streamReader.ReadLine();
                    string[] strings = String.Split(",");
                    Student student=new Student(Convert.ToInt32(strings[0]),strings[1],strings[2]);
                    students.Add(student);
                }
                for (int i = students.Count - 1; i >= 0; i--)
                {
                    if (((Student)students[i]).Id==id)
                    {
                        students.RemoveAt(i);
                    }
                }
                streamReader.Close();

                StreamWriter streamWriter=new StreamWriter("aaa/Student.txt",false);
                foreach (Student student in students)
                {
                    string String =student.Id+ ","+student.Name+","+student.Age;
                    streamWriter.WriteLine(String);
                }
                streamWriter.Close();
                return true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return false;
            }
        }


        public bool updateStudentDao(Student updateStudent)
        {
            try
            {
                StreamReader streamReader=new StreamReader("aaa/Student.txt");
                List<Student> students=new List<Student>();
                while (!streamReader.EndOfStream)
                {
                    string String=streamReader.ReadLine();
                    string[] strings = String.Split(",");
                    Student student=new Student(Convert.ToInt32(strings[0]),strings[1],strings[2]);
                    students.Add(student);
                }
                students.ForEach(student => {
                   if (student.Id==updateStudent.Id)
                   {
                       student.Id = updateStudent.Id;
                       student.Name = updateStudent.Name;
                       student.Age = updateStudent.Age;
                   }
               });
                streamReader.Close();
            
                StreamWriter streamWriter=new StreamWriter("aaa/Student.txt",false);
                foreach (Student student in students)
                {
                    string String =student.Id+ ","+student.Name+","+student.Age;
                    streamWriter.WriteLine(String);
                }
                streamWriter.Close();
                return true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return false;
            }
        }

        public ArrayList selectStudentDao()
        {
            try
            {
                StreamReader streamReader=new StreamReader("aaa/Student.txt");
                ArrayList students=new ArrayList();
                while (!streamReader.EndOfStream)
                {
                    string String=streamReader.ReadLine();
                    string[] strings = String.Split(",");
                    Student student=new Student(Convert.ToInt32(strings[0]),strings[1],strings[2]);
                    students.Add(student);
                }
                streamReader.Close();
                return students;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                // throw;
                return null;
            }
        }
    }
}